﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace BuildServer.Models
{
    public class EditPropertyModel
    {
        public EditPropertyModel()
        {
        }

        public EditPropertyModel(BuildDefinition buildDefinition)
        {
            this.id = buildDefinition.Id;
            this.Name = buildDefinition.Name;
            this.Load(buildDefinition);
        }

        public int id { get; set; }

        public string Name { get; set; }

        public List<EditProperty> Properties { get; set; }

        private void Load(BuildDefinition buildDefinition)
        {
            // get build definition properties
            var xmlTemplate = buildDefinition.BuildTemplate.BuildProps;
            var buildProperties = buildDefinition.BuildProperties.ToList();

            // parse xml
            var result = new List<EditProperty>();
            XDocument document = XDocument.Parse(xmlTemplate);
            XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";

            // process property groups
            foreach (var propertyGroupElement in document.Root.Elements(msbuild + "PropertyGroup"))
            {
                string group = (string)propertyGroupElement.Attribute("Label") ?? string.Empty;
                foreach (var propertyElement in propertyGroupElement.Elements())
                {
                    string name = propertyElement.Name.LocalName;
                    string options = (string)propertyElement.Attribute("Options");
                    result.Add(new EditProperty
                    {
                        Group = group,
                        Label = (string)propertyElement.Attribute("Label") ?? name,
                        Name = propertyElement.Name.LocalName,
                        Value = buildDefinition.GetBuildProperty(name) ?? propertyElement.Value,
                        DefaultValue = propertyElement.Value,
                        Options = string.IsNullOrEmpty(options) ? null : options.Split(';')
                    });
                }
            }

            // done
            Properties = result;
        }
    }

    public class EditProperty
    {
        /// <summary>
        /// Property group label
        /// </summary>
        public string Group { get; set; }

        /// <summary>
        /// Property Name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Display label
        /// </summary>
        public string Label { get; set; }

        /// <summary>
        /// Current value
        /// </summary>
        public string Value { get; set; }

        /// <summary>
        /// Default value
        /// </summary>
        public string DefaultValue { get; set; }

        /// <summary>
        /// Possible values
        /// </summary>
        /// <remarks>
        /// Free text if this is null reference
        /// </remarks>
        public string[] Options;
    }
}